#include <stdio.h>
#include <string.h>
#include "symbolTable.h"
#include "portal.y.c"

char* itoa(int i, char b[]){
    char const digit[] = "0123456789";
    char* p = b;
    if(i<0){
        *p++ = '-';
        i = -1;
    }
    int shifter = i;
    do{ //Move to where representation ends
        ++p;
        shifter = shifter/10;
    }while(shifter);
    *p = '\0';
    do{ //Move back, inserting digits as u go
        *--p = digit[i%10];
        i = i/10;
    }while(i);
    return b;
}

void strUpper(char *string){
	int tamanho = strlen(string);
	int i;
	for(i= 0;i < tamanho; i++)
	{
		string[i] = toupper(string[i]);
	}
}

t_tipos returnType(const char* string){
	if(!strcmp(string,"inteiro"))
		return INTEIRO;
	else if(!strcmp(string,"real"))
		return REAL;
	else if(!strcmp(string,"caracter"))
		return CARACTER;
	else if(!strcmp(string,"texto"))
		return TEXT0;
	else if(!strcmp(string,"registro"))
		return REGISTRO;
	else if(!strcmp(string,"matriz"))
		return MATRIZ;
	else if(!strcmp(string,"booleano"))
		return BOOLEANO;
	else if(!strcmp(string,"arquivo"))
		return ARQUIVO;
	else
		return -1;
}

symbolTable* createItem() {
	symbolTable *new;
	
	new = (symbolTable*) malloc(sizeof(symbolTable));
	new->next = NULL;
	new->prev = NULL;
	return new;
}

int isEmpty(symbolTable *table){
	if(table == NULL) return 1;
	else return 0;
}


int isAdded(char str[64]) {
	symbolTable *p;
	p = head;
	while (p != NULL) {
		if (!strcmp(p->name, str)) {
			return 1;
		}
		p = p->next;
	}
	return 0;
}

int printTable(symbolTable *table) {
	symbolTable *p;
	p = table;
	int i = 0;
	
	printf("INICIO\n");
	while (p != NULL) {
		printf("Item: %d\tNome: %s\tTipo: %d\tClasse: %d\t num_mat: %d\n",i,p->name, p->type, p->class, p->numero_matriz);
		i++;
		p = p->next;
	}
	printf("FIM\n");
	return 0;
}

int add(symbolTable **table, char name[64], t_classes classe, t_tipos tipo, int numero_matriz) {
	symbolTable *new;

	if (isEmpty(*table)) {
		new = createItem();
		strcpy(new->name,name);
		new->class = classe;
		new->type = tipo;
		new->numero_matriz = numero_matriz;
		(*table) = new;
		return 0;
	}
	else {
		if (!isAdded(name)) {
			new = createItem();
			//insere valor do nome
			strcpy(new->name,name);
			new->class = classe;
			new->type = tipo;
			new->numero_matriz = numero_matriz;
			symbolTable *p = (*table);
			while (p->next != NULL)
				p = p->next; 
			(p)->next = new;
			new->prev = p;
			p = new;
			return 0;
		}
	}
	return -1;
}

symbolTable* search(char name[64]) {
	symbolTable *p;
	p = head;
	while (p != NULL) {
		if (!strcmp(p->name, name)) {
			return p;
		}
		p = p->next;
	}
	return NULL;
}

char* returnTypeChar(t_tipos type){

	switch (type) {
        case INTEIRO:
            return "inteiro";
		case REAL:
			return "real";
		case MATRIZ:
			return "matriz";
		default:
            return "";
    }
}

t_tipos returnTypeTeste(const char* string){
	printf("string: %s\n", string);
	if(!strcmp(string,"inteiro"))
		return INTEIRO;
	else if(!strcmp(string,"real"))
		return REAL;
	else if(!strcmp(string,"caracter"))
		return CARACTER;
	else if(!strcmp(string,"texto"))
		return TEXT0;
	else if(!strcmp(string,"registro"))
		return REGISTRO;
	else if(!strcmp(string,"matriz"))
		return MATRIZ;
	else if(!strcmp(string,"booleano"))
		return BOOLEANO;
	else if(!strcmp(string,"arquivo"))
		return ARQUIVO;
	else
		return -1;
}

void value_get(symbolTable *value, t_tipos type, void *val){

/*	if (type == MATRIZ) {*/
/*		*((int **) val) = value->value.matriz;*/
/*	}*/
	
}

void value_set(symbolTable *value, t_tipos type, void *val){

	if (type == MATRIZ) {
/*       value->value.matriz = *((int **) val);*/
	}
	
}

void value_set_from_matriz(symbolTable *value, int *val){

    value_set(value, MATRIZ, ((void *) &val));

}

int main() {
	head = NULL;
	printf("Comecou");
	mainParser();
	printf("\nAcabou\n");
	return 0;
}
	

